﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Globalization;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Common;
using MockPrjTeam4.Entity;

namespace MockPrjTeam4.Pages.Views
{
    public partial class ProgrammesList22 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetListProgramme();
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Forms/AddProgramme.aspx");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtProName.Text = "";
        }

        protected void btnNone_Click(object sender, EventArgs e)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void chkInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (hdffilter.Value != "")
            {
                Search(hdffilter.Value);
            }
            else
            {
                if (txtProName.Text != "")
                {
                    IList<Programmes> proFilter;
                    if (chkInactive.Checked)
                    {
                        proFilter = ProgrammeBiz.SearchPro(txtProName.Text, true);
                    }
                    else
                    {
                        proFilter = ProgrammeBiz.SearchPro(txtProName.Text, false);
                    }
                    gvListProgramme.DataSource = proFilter;
                    gvListProgramme.DataBind();
                }
                else
                {
                    GetListProgramme();
                }
            }
        }

        private void Search(string commandName)
        {
            IList<Programmes> pro = new List<Programmes>();
            string isActive = "";
            if (chkInactive.Checked)
            {
                isActive = "true";
            }
            else
            {
                isActive = "false";
            }
            switch (commandName)
            {
                case "SearchAll":
                    {
                        if (isActive == "true")
                            pro = ProgrammeBiz.GetListProgramme();
                        else
                            pro = ProgrammeBiz.GetListActiveProgrammes();
                        break;
                    }
                case "Search09":
                    {
                        //range = "[0-9]";
                        pro = ProgrammeBiz.Search("[0-9]%", isActive);
                        break;
                    }
                case "SearchAE":
                    {
                        pro = ProgrammeBiz.Search("[A-E]%", isActive);
                        break;
                    }
                case "SearchFJ":
                    {
                        pro = ProgrammeBiz.Search("[F-J]%", isActive);
                        break;
                    }
                case "SearchKN":
                    {
                        pro = ProgrammeBiz.Search("[K-N]%", isActive);
                        break;
                    }
                case "SearchOR":
                    {
                        pro = ProgrammeBiz.Search("[O-R]%", isActive);
                        break;
                    }
                case "SearchSV":
                    {
                        pro = ProgrammeBiz.Search("[S-V]%", isActive);
                        break;
                    }
                case "SearchWZ":
                    {
                        pro = ProgrammeBiz.Search("[W-Z]%", isActive);
                        break;
                    }
            }
            if (pro.Count == 0)
            {
                lblMessageResult.Text = "No more results";
            }
            else
            {
                lblMessageResult.Text = "";
            }
            gvListProgramme.DataSource = pro;
            gvListProgramme.DataBind();
        }

        protected void Click(object sender, CommandEventArgs e)
        {
            hdffilter.Value = e.CommandName;
            Search(e.CommandName);
        }

        private void GetListProgramme()
        {
            IList<Programmes> pro;
            if (chkInactive.Checked)
            {
                pro = ProgrammeBiz.GetListProgramme();
            }
            else
            {
                pro = ProgrammeBiz.GetListActiveProgrammes();
            }
            gvListProgramme.DataSource = pro;
            gvListProgramme.DataBind();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IList<Programmes> pro;
            if (chkInactive.Checked)
            {
                pro = ProgrammeBiz.SearchPro(txtProName.Text, true);
            }
            else
            {
                pro = ProgrammeBiz.SearchPro(txtProName.Text, "true");
            }

            gvListProgramme.DataSource = pro;
            gvListProgramme.DataBind();
            if (pro == null)
            {
                lblMessageResult.Text = "No more results";
            }

        }

        

        protected void gvListProgramme_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvListProgramme.PageIndex = e.NewPageIndex;
            gvListProgramme.DataBind();
        }

        protected void lkbtnProName_Click(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                int proId = Methods.ConvertNumber(e.CommandName);
                ProgrammeBiz.SetProActive(proId);
            }
            Response.Redirect(string.Format("../Forms/EditProgramme.aspx?ID={0}", e.CommandName));
        }
    }
}